import axios from 'axios'
import { Dialog } from 'vant';
import router from '../router'
let http = axios.create({
  baseURL: process.env.NODE_ENV == 'production' ? 'http://backend-api-01.newbee.ltd/api/v1/' : ''
})
http.interceptors.request.use((config) => {
  // 在拦截器这里添加 请求头 token
  config.headers.token = localStorage.getItem('shop_token')
  return config
})

http.interceptors.response.use((response) => {
  if (response.data.resultCode != 200) {
    if (response.data.resultCode == 416) {
      // 登录过期或者没登陆

      Dialog.alert({
        title: '提示',
        message: response.data.message,
      }).then(() => {
        // on close
        // router.push('/login?from='+)
        router.push('/login?canback=1')
      });
    } else {
      Dialog.alert({
        title: '提示',
        message: response.data.message,
      })
    }
  }
  return response.data
}, function (err) {
  return Promise.reject(err)
})

export default http